<?php
class MY_Model extends CI_Model {	
    var $table_name;
    var $primary_key;
    function get_list($start=null,$limit=null,$sort= '',$dir='DESC',$where = null,$fields = '*')
    {
    	$this->db->select($fields);
		$this->db->from($this->table_name);
		if(!empty($where)) $this->db->where($where);
    	if(empty($sort)) $this->db->order_by($this->primary_key, $dir);
    	else $this->db->order_by($sort, $dir);

    	if(!empty($limit)) $this->db->limit($limit,$start);
    	
    		
	    $query = $this->db->get();

		return $query->result_array();
        
    }
    function get_all(){
    	$query = $this->db->get($this->table_name);
    	return $query->result_array();
    }
    function get_total($where=null)
    {
        $this->db->from($this->table_name);
        if(!empty($where)) $this->db->where($where);
        return $this->db->count_all_results();
    }
    function insert($data)
    {
        $this->filterData($data);
        $this->db->insert($this->table_name, $data);
        //$this->db->cache_delete_all();
        return $this->db->insert_id()  ;
    }

    function get_by_id($id)
    {
        $this->db->where($this->primary_key,$id);
            $query = $this->db->get($this->table_name);
        return $query->row_array();        
    }

    function get_by_field($field_value=array(),$fields_to_get='*',$get_row = false)//$field_value = array('field_name'=>'value');
    {
        //$this->db->cache_on();
    	$this->db->select($fields_to_get);
    	$this->db->from($this->table_name);
    	foreach($field_value as $key=>$value){
            $this->db->where($key,$value);
    	}
        $query = $this->db->get();
        if($get_row==true) return $query->row_array();
        else return $query->result_array();        
    }
	function check_isset($field_value=array()){		
		$this->db->from($this->table_name);
		foreach($field_value as $key=>$value){
            $this->db->where($key,$value);
    	}
  		$total = $this->db->count_all_results();  	
        if($total > 0)
			return true;
		else return false;	
    }    
    
    function update($data,$id)
    {
        $this->filterData($data);
        //$this->db->cache_delete_all();
        return $this->db->update($this->table_name, $data, array($this->primary_key => $id));
    }
    function delete($id){
        //$this->db->cache_delete_all();
    	return $this->db->delete($this->table_name, array($this->primary_key => $id)); 
    }
	function delete_by_field($field_value=array()){//$field_value = array('field_name'=>'value'); 	
    	return $this->db->delete($this->table_name, $field_value); 
    }
    function get_max($field,$where=null){
        $name = "{$this->table_name}_$field";
        $this->db->select_max($field,$name);
        $this->db->from($this->table_name);
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        $query = $this->db->get();
        $row = $query->row_array();
        return $row[$name];
    }
    protected function filterData(&$data){
        foreach($data as $key=>$row){
            if (!$this->db->field_exists($key, $this->table_name))
            {
                unset($data[$key]);
            }else{
                $data[$key] = trim($row);
            }
        }
    }
}